package com.yz.xyt.service.impl;

import com.yz.xyt.domain.AppManager;
import com.yz.xyt.mapper.AppManagerMapper;
import com.yz.xyt.service.IAppManagerService;
import com.yz.xyt.utils.DateUtils;
import com.yz.xyt.utils.FileUploadUtils;
import com.yz.xyt.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@Service
public class AppManagerServiceImpl implements IAppManagerService {


    @Value("${xyt.appUpload}")
    private String uploadDir;

    @Autowired
    AppManagerMapper appManagerMapper;

    @Override
    public int findAppInfo() {
        return 0;
    }

    @Transactional
    @Override
    public int saveAppInfo(MultipartFile multipartFile) {
        int count = 0;
        String originalFilename = multipartFile.getOriginalFilename();
        String version =  originalFilename.substring(originalFilename.indexOf("_")+1,originalFilename.lastIndexOf("."));
        AppManager recordAppManager = findAppVersion();
        String appName = originalFilename.substring(0,originalFilename.lastIndexOf("."));
        //应用后缀
        String suffix = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);

        try {
            AppManager appManager = new AppManager();
            appManager.setAppName(appName);
            appManager.setAppVersion(version);
            appManager.setPath(uploadDir + "/" + appName);

            if(recordAppManager == null){
                appManager.setCreateBy(SecurityUtils.getUsername());
                appManager.setCreateTime(DateUtils.getNowDate());
                count = appManagerMapper.insert(appManager);
                multipartFile.transferTo(FileUploadUtils.getAbsoluteFile(uploadDir,originalFilename));
            } else{
                if(!version.equals(recordAppManager.getAppVersion())){
                    appManager.setId(recordAppManager.getId());
                    appManager.setUpdateBy(SecurityUtils.getUsername());
                    appManager.setUpdateTime(DateUtils.getNowDate());
                    count = appManagerMapper.updateByPrimaryKeySelective(appManager);
                }
                deleteOldApp(suffix,recordAppManager.getPath());
                multipartFile.transferTo(FileUploadUtils.getAbsoluteFile(uploadDir,originalFilename));
            }
        } catch (IOException e) {
            throw new RuntimeException("应用上传失败,IO异常");
        }
        return count;
    }

    @Override
    public int updateAppInfo(AppManager appManager) {
        return 0;
    }

    @Override
    public AppManager findAppVersion() {
        AppManager appManager = appManagerMapper.findAppVersion();
        return appManager;
    }

    private void deleteOldApp(String suffix,String uploadDir){
        File file = new File(uploadDir + "." + suffix);
        if (file.exists()){
            file.delete();
        }
    }
}
